package org.iabako.client.ui.page.business.product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigSearchEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.ui.form.business.product.ProductSearchForm;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.client.ui.page.AbstractSearchPage;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.enumeration.NotificationType;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.iservice.business.ProductServiceAsync;
import org.iabako.shared.iservice.generic.CRUDServiceAsync;
import org.iabako.shared.iservice.generic.SearchServiceAsync;
import org.iabako.shared.iservice.generic.SuggestServiceAsync;

/**
 * Created by jose on 15/01/15.
 */
public class ProductSearch extends AbstractSearchPage {

    interface MyUiBinder extends UiBinder<HTMLPanel, ProductSearch> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    @UiField
    HTMLPanel searchPanel;
    @UiField
    HTMLPanel resultsPanel;

    public ProductSearchForm productSearchForm;

    public ProductSearch(boolean defaultDataTable) {
        super(defaultDataTable);

        if (this instanceof StockManagement) {

            LabelText comeBackToProducts = new LabelText("pageactionlink");
            comeBackToProducts.setStyleClass("linklabel");
            comeBackToProducts.setLabel("stock_come_back_to_products");
            comeBackToProducts.setClassIcon("gobackicon");

            page.actionButtons.add(comeBackToProducts);
            comeBackToProducts.getLabel().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    NavigationController.getInstance().goTo(PageEnum.ProductSearch);
                }
            });
        } else {

            Button createProduct = new Button("pageactionbutton", "product_create_title");
            page.actionButtons.add(createProduct);

            createProduct.getButton().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    NavigationController.getInstance().goTo(PageEnum.ProductCreate);
                }
            });

            if (!AuthenticateUserProvider.getUser().isNoStockAccess()) {
                Button stockManagement = new Button("pageactionbutton", "stock_management_title");
                page.actionButtons.add(stockManagement);
                stockManagement.getButton().addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        NavigationController.getInstance().goTo(PageEnum.StockManagement);
                    }
                });
            }
        }
    }

    @Override
    protected void dataTableRemoveCustomAction() {
        AuthenticateUserProvider.getNotificationPanel().refresh(NotificationType.stockRupture, NotificationType.stockLowerThanLimit,
                NotificationType.newPastInstallments, NotificationType.futureInstallments, NotificationType.paymentRequest);
        AuthenticateUserProvider.getTrackingPanel().refresh(TrackingType.productDelete);
    }

    @Override
    protected HTMLPanel getContentPanel() {
        return binder.createAndBindUi(this);
    }

    @Override
    protected SearchServiceAsync getSearchServiceAsync() {
        return ProductServiceAsync.Util.getInstance();
    }

    @Override
    protected CRUDServiceAsync getCRUDServiceAsync() {
        return ProductServiceAsync.Util.getInstance();
    }

    @Override
    protected SuggestServiceAsync getSuggestServiceAsync() {
        return ProductServiceAsync.Util.getInstance();
    }

    @Override
    protected HTMLPanel getResultsPanel() {
        return resultsPanel;
    }

    @Override
    protected HTMLPanel getSearchPanel() {
        return searchPanel;
    }

    @Override
    protected Composite getSearchForm(DataTable dataTable) {
        if (productSearchForm == null) {
            productSearchForm = new ProductSearchForm(new DefaultSuggestHandler(getSuggestServiceAsync()), dataTable);
        }
        return productSearchForm;
    }

    @Override
    protected PageConfigSearchEnum getSearchPageConfigEnum() {
        return PageConfigSearchEnum.ProductSearch;
    }

    @Override
    protected PageEnum getModificationPage() {
        return PageEnum.ProductModify;
    }
}
